package com.axjy.web;

import com.axjy.pojo.User;
import com.axjy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by l on 2018/1/20.
 */
@Controller
@RequestMapping("/user")
public class UserHandler {

    @Resource
    private UserService userService;

    @GetMapping("/list")
    public String list(String userCode,String userName,HttpServletRequest req){
        List<User> list = userService.search(userCode,userName);
        req.setAttribute("list",list);
        return "user/list";
    }

    @GetMapping("/add")
    public String add(){
        return "user/add";
    }

    @PostMapping("/addUser")
    public String addUser(User user,HttpSession session){
        userService.add(user,session);
        return "redirect:/user/list";
    }

    @GetMapping("/del")
    public String del(Integer id){
        userService.delUser(id);
        return "redirect:/user/list";
    }

    @GetMapping("/update")
    public String update(Integer id, Model model){
        User user=userService.get(id);
        model.addAttribute("user",user);
        return "user/update";
    }

    @PostMapping("/updateUser")
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:/user/list";
    }
}
